import 'dart:io';

import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import '../utils/common_page_config.dart';
import 'button_widget.dart';
class ImageWidget extends StatelessWidget {
  final File? file;
  final String? name;
  final String? url;
  final double? width;
  final double? height;
  final BoxFit? fit;
  final Color? color;
  final Widget? errorWidget;
  final bool? again;
  final String? package;
  final bool? gaplessPlayback;
  final int? maxBytes;
  final bool? cache;
  const ImageWidget({
    Key? key,
    this.file,
    this.name,
    this.url,
    this.width,
    this.height,
    this.fit,
    this.color,
    this.errorWidget,
    this.again,
    this.package,
    this.gaplessPlayback,
    this.maxBytes,
    this.cache,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return file != null
        ? ExtendedImage.file(
            file!,
            width: width,
            height: height,
            fit: fit,
            color: color,
            loadStateChanged: loadStateChanged(),
          )
        : name != null && name!.isNotEmpty
            ? ExtendedImage.asset(
                name!,
                width: width,
                height: height,
                fit: fit,
                color: color,
                loadStateChanged: loadStateChanged(),
                package: package,
                gaplessPlayback: gaplessPlayback ?? true,
                maxBytes:maxBytes ?? 200*1024,
              )
            : url != null && url!.isNotEmpty
                ? ExtendedImage.network(
                    url!,
                    width: width,
                    height: height,
                    fit: fit,
                    color: color,
                    cache: cache ?? true,
                    gaplessPlayback: gaplessPlayback ?? false,
                    loadStateChanged: loadStateChanged(),
                  )
                : errorWidget ??
                    commonPageConfig.failedWidgetBuilder(width, height, fit);
  }

  LoadStateChanged loadStateChanged() {
    return (ExtendedImageState state) {
      switch (state.extendedImageLoadState) {
        case LoadState.loading:
          return Container(
            width: width,
            height: height,
            alignment: Alignment.center,
            child: CupertinoActivityIndicator(
              radius: width != null && height != null
                  ? width! > height!
                      ? height! / 4
                      : width! / 4
                  : width != null && width != double.infinity
                      ? width! / 4
                      : height != null && height != double.infinity
                          ? height! / 4
                          : 10,
            ),
          );
        case LoadState.completed:
          return ExtendedRawImage(
            image: state.extendedImageInfo?.image,
            width: width,
            height: height,
            fit: fit,
            color: color,
          );
        case LoadState.failed:
          return errorWidget ??
              ((again ?? true)
                  ? ButtonWidget(
                      onPressed: () {
                        state.reLoadImage();
                      },
                      child: commonPageConfig.failedWidgetBuilder(
                          width, height, fit),
                    )
                  : commonPageConfig.failedWidgetBuilder(width, height, fit));
      }
    };
  }
}
